package com.cat.dataStructure02;

import java.util.*;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/contest/weekly-contest-425/problems/rearrange-k-substrings-to-form-target-string/https://leetcode.cn/contest/weekly-contest-425/problems/rearrange-k-substrings-to-form-target-string/
 * @create 2025/10/14 15:21
 * @since JDK17
 */

public class Solution17 {
    public boolean isPossibleToRearrange(String s, String t, int k) {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        int n = s.length(), m = n / k;
        for (int i = 0; i < n; i += m) {
            list1.add(s.substring(i, i + m));
            list2.add(t.substring(i, i + m));
        }
        Collections.sort(list1);
        Collections.sort(list2);
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).compareTo(list2.get(i)) != 0) {
                return false;
            }
        }
        return true;
    }
}
